/**
 * 工具类定义模式
 *
 * export const 可以英汉互译的工具方法名 = ()=>{}
 *
 * 注意：具类的，注释，参数，返回值，不是太着急的话，建议写的清楚一点
 * 注意：具类的，注释，参数，返回值，不是太着急的话，建议写的清楚一点
 * 注意：具类的，注释，参数，返回值，不是太着急的话，建议写的清楚一点
 */
import {Md5} from "ts-md5";

/**
 * 返回字符串的MD5
 * @param str
 */
export const md5 = (str: any) => {
    return Md5.hashStr(str).toString();
}

/**
 * 拷贝对象
 * @param obj
 */
export const copy = (obj: any) => {
    // let newModel = Object.assign({}, obj);
    // return newModel;
    if (obj) {
        return JSON.parse(JSON.stringify(obj));
    }
    return null;
};

/**
 * 返回是否为空
 * @param obj
 */
export const isBlank = (obj: any) => {
    if (obj == null || obj == undefined) {
        return true;
    }
    return obj.toString().trim() == "";

};

/**
 * 返回是否为非空
 * @param obj
 */
export const isNotBlank = (obj: any) => {
    return (!isBlank(obj))
}

/**
 * 整理服务器返回的路由
 *
 * @return mMenus 整理后的完整父子路由数组
 * @return menusPath 全部path Set集合
 */
export const arrangeMenu = (menus: any) => {
    let menusPath: any = [];
    let mMenus: any = [];
    if (menus) {
        for (let m of menus) {
            let menu = JSON.parse(JSON.stringify(m)) as any;
            menu.meta = {};
            if (menu.hidden) {
                menu.meta.hidden = menu.hidden;
                delete menu["hidden"];
            }
            if (menu.title) {
                menu.meta.title = menu.title;
                delete menu["title"];
            }
            if (menu.icon) {
                menu.meta.icon = menu.icon;
                delete menu["icon"];
            }
            if (menu.alwaysShow) {
                menu.meta.alwaysShow = menu.alwaysShow;
                delete menu["alwaysShow"];
            }
            if (menu.addTag) {
                menu.meta.addTag = menu.addTag;
                delete menu["addTag"];
            }
            if (menu.type) {
                menu.meta.type = menu.type;
                delete menu["type"];
            }
            if (menu.affix) {
                menu.meta.affix = menu.affix;
                delete menu["affix"];
            }
            if (menu.sort) {
                menu.meta.sort = menu.sort;
                delete menu["sort"];
            }

            delete menu["createUid"];
            delete menu["createTime"];
            delete menu["updateUid"];
            delete menu["updateTime"];
            delete menu["dataStates"];
            if (!menu.redirect) {
                delete menu["redirect"];
            }
            if (menu.path) {
                menusPath.push(menu.path);
            }
            mMenus.push(menu);
        }
    }

    //console.log("menus", menus)
    //console.log("mMenus", mMenus)
    //console.log("menusPath", menusPath)
    return {mMenus: mMenus, menusPath: menusPath};
}

/**
 * 按照对象的某个属性排序
 * 用法: 【对象数组】.sort(compareProps('id'));
 */
export const compareProps = (key: any) => {
    return (obj1: any, obj2: any) => {
        const val1 = obj1[key];
        const val2 = obj2[key];
        if (val1 < val2) {
            return -1;
        }
        if (val1 > val2) {
            return 1;
        }
        return 0;
    };
};

//
export const menuTreeToArray = (list: any, newArr: any, parentPath: any = null) => {
    list.forEach((item: any) => {
        item.parentPath = parentPath;
        const {children} = item;
        if (children) {
            delete item.children;
            if (children.length) {
                menuTreeToArray(children, newArr, item.path);
            }
        }
        newArr.push(item);
    });
    return newArr;
};

/**
 * 清空数组
 */
export const clearArray = (array: any) => {
    if (array && array.length > 0) {
        return array.splice(0, array.length);
    }
}


export const tree = (obj: any): Array<any> => {
    let tree = getTree(obj);
    return JSON.parse(tree);
}

/**
 * 排版无线层级路由
 * @param list
 */
export const getTree = (list: any) => {

    //根节点数组
    let genArray = new Array<any>();
    //非根节点数组
    let childArray = new Array<any>();

    for (let key in list) {
        let listElement = list[key];
        if (listElement["pid"] == "0") {
            genArray.push(listElement);
        } else {
            childArray.push(listElement);
        }
    }
    //非根节点数组
    let routerArray = new Array<any>();
    for (let key in genArray) {
        routerArray.push(sort(genArray[key], childArray));
    }
    return JSON.stringify(routerArray)
};

/**
 * 排版无线层级路由
 * @param parent_obj
 * @param allList
 */
export const sort = (parent_obj: any, allList: Array<any>) => {
    let child_list = new Array<any>();
    for (let i = 0; i < allList.length; i++) {
        let now_obj = allList[i];
        if (now_obj) {
            let now_pid: string = now_obj["pid"];
            // try {
            //     //////console.log(now_obj)
            //     //////console.log(JSON.stringify(allList))
            //     let aaa = now_pid.toString()
            // } catch (e) {
            //     debugger
            // }
            //    查得到
            if (now_pid.toString() == (parent_obj["id"] + "")) {
                child_list.push(now_obj);//同级别添加
                sort(now_obj, allList);//继续递归
            }
            //不同级别不做处理
            else {
            }
            if (child_list.length != 0) {
                parent_obj["children"] = child_list;
            }
        }
    }
    return parent_obj
};



